package com.hy.admin.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hy.admin.entity.YangCardEntity;
import com.hy.admin.helper.request.PersonalRequest;
import com.hy.admin.helper.request.YangCardRequest;
import com.hy.admin.helper.response.OrderResponse;
import com.hy.admin.helper.response.PersonalResponse;
import com.hy.admin.service.PersonalService;
import com.hy.admin.service.YangCardOrderService;
import com.hy.admin.service.YangCardService;
import com.hy.core.request.ApiRequestDTO;
import com.hy.core.response.ApiResponseDTO;
import com.hy.core.response.ServicesResult;
import org.apache.commons.httpclient.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by yangqq on 18/4/3.
 */
@RestController
@RequestMapping("/api/card")
public class YangCardController {

    protected final Logger logBase = LoggerFactory.getLogger(YangCardController.class);
    @Autowired
    private YangCardService yangCardService;
    @Autowired
    private YangCardOrderService yangCardOrderService;

    /**
     * 氧卡列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResponseDTO list(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        //获取请求数据
        ServicesResult<List<YangCardEntity>> yangCardResponse = yangCardService.list();
        apiResponseDTO.initServicesResult(yangCardResponse);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(yangCardResponse.getResult()));
        logBase.info("yang_card_list_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 氧卡详情
     * @param request
     * @return
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public ApiResponseDTO detail(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("yang_card_detail_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        //获取请求对象
        YangCardRequest yangCardRequest= gson.fromJson(apiRequestDTO.getRequestData(), YangCardRequest.class);
        //获取请求数据
        ServicesResult<YangCardEntity> yangCardResponse = yangCardService.detail(yangCardRequest.getCardId());
        apiResponseDTO.initServicesResult(yangCardResponse);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(yangCardResponse.getResult()));
        logBase.info("yang_card_detail_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 购买氧卡
     * @param request
     * @return
     */
    @RequestMapping(value = "/buy", method = RequestMethod.POST)
    public ApiResponseDTO buy(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("yang_card_buy_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        //获取请求对象
        YangCardRequest yangCardRequest= gson.fromJson(apiRequestDTO.getRequestData(), YangCardRequest.class);
        //获取请求数据
        ServicesResult<OrderResponse> yangCardResponse = yangCardOrderService.buy(yangCardRequest.getMemberId(), yangCardRequest.getCardId());
        apiResponseDTO.initServicesResult(yangCardResponse);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(yangCardResponse.getResult()));
        logBase.info("yang_card_buy_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

}
